<?php

class module_api {

    public $error_msg = '';
    private $installdir, $module;

    /**
     * 模块安装
     * @param string $module 模块名
     */
    public function install($module) {
        if ($module) {
            $this->module = $module;
        }
        $this->installdir = APP_PATH . $this->module . DS . 'install' . DS;
        $this->check();
        $model = include($this->installdir . 'model.php');
        foreach ($model as $m) {
            $sql = file_get_contents($this->installdir . $m . '.sql');
            $this->sql_execute($sql);
        }
        if (is_file($this->installdir . 'extention.php')) {
            $menu = db('menu');
            include ($this->installdir . 'extention.php');
        }
        $arr = require $this->installdir . 'config.php';
        $arr['installdate'] = date('Y-m-d');
        db('module')->insert($arr, true, false);
        return true;
    }

    /**
     * 模块卸载
     * @param string $module 模块名
     */
    public function uninstall($module) {
        if (!$module) {
            $this->error_msg = '模块名称为空！';
            return false;
        }
        $this->uninstalldir = APP_PATH . $module . DS . 'uninstall' . DS;
        if (!is_dir($this->uninstalldir)) {
            $this->error_msg = 'uninstall目录不存在！';
            return false;
        }
        if (!is_file($this->uninstalldir . 'model.php')) {
            $this->error_msg = 'model.php不存在！';
            return false;
        }
        $model = include($this->uninstalldir . 'model.php');
        if (is_array($model) && !empty($model)) {
            foreach ($model as $m) {
                db()->query('DROP TABLE IF EXISTS ' . db()->prefix . $m . '`');
            }
        }
        db('module')->delete(array('module' => $module));
        if (is_file($this->uninstalldir . 'extention.php')) {
            $menu = db('menu');
            include ($this->uninstalldir . 'extention.php');
        }
        return true;
    }

    /**
     * 检查安装目录
     * @param string $module 模块名
     */
    public function check($module = '') {
        if ($module)
            $this->module = $module;
        if (!$this->module) {
            $this->error_msg = '模块名为空！';
            return false;
        }

        if (db('module')->where(array('module' => $this->module))->find()) {
            $this->error_msg = $this->module . '模块已经安装过！';
            return false;
        }

        if (!$this->installdir) {
            $this->installdir = APP_PATH . $this->module . DS . 'install' . DS;
        }

        if (!is_dir($this->installdir)) {
            $this->error_msg = 'install目录不存在！';
            return false;
        }

        if (!is_file($this->installdir . 'config.php')) {
            $this->error_msg = 'config.php不存在！';
            return false;
        }

        if (!is_file($this->installdir . 'model.php')) {
            $this->error_msg = 'model.php不存在！';
            return false;
        }

        $model = include($this->installdir . 'model.php');
        if (is_array($model) && !empty($model)) {
            foreach ($model as $m) {
                if ($m && !is_file($this->installdir . $m . '.sql')) {
                    $this->error_msg = $m . '.sql不存在！';
                    return false;
                }
            }
        }
        return true;
    }

}
